package net.learn.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.learn.framework.common.page.PageResult;
import net.learn.framework.common.utils.Result;
import net.learn.system.entity.*;
import net.learn.system.query.SysMyExamQuery;
import net.learn.system.query.SysMyPaperQuery;
import net.learn.system.service.*;
import net.learn.system.vo.SysMyExamVO;
import net.learn.system.vo.SysMyPaperVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的考试
 */
@RestController
@RequestMapping("sys/myexam")
@Tag(name="我的考试")
@AllArgsConstructor
public class SysMyExamController {
    @Autowired
    private final SysMyExamService myExamService;
    @Autowired
    private final SysRadioService radioService;
    @Autowired
    private final SysRadioDetailService radioDetailService;
    @Autowired
    private final SysCheckBoxService checkBoxService;
    @Autowired
    private final SysCheckBoxDetailService checkBoxDetailService;
    @Autowired
    private final SysJudgeService judgeService;
    @Autowired
    private final SysJudgeDetailService judgeDetailService;

    @Autowired
    private final SysFillService fillService;

    @Autowired
    private final SysAnswerService answerService;

    @GetMapping("getMyExam")
    @Operation(summary = "分页获取我的考试列表")
    public Result<PageResult<SysMyExamVO>> getMyExam(@Valid SysMyExamQuery query){
        PageResult<SysMyExamVO> page = myExamService.qryMyExamListPage(query);

        return Result.ok(page);
    }
    @PostMapping("getMyExamList")
    @Operation(summary = "获取我的考试列表")
    public Result<List<SysMyExamVO>> getMyExamList(@RequestBody SysMyExamQuery query){
        List<SysMyExamVO> list = myExamService.qryMyExamQuestionList(query);

        return Result.ok(list);
    }

    @PostMapping("updateMyExamById")
    @Operation(summary = "批改考试成绩")
    public ResponseEntity updateMyExamById(@RequestBody SysMyExamVO qry) {
        Map<String, Object> vo = new HashMap<>();
        try{
            if (qry.getQuestionType()==4){
                QueryWrapper<SysFillEntity> fillwrapper = new QueryWrapper<>();
                fillwrapper.eq("id",qry.getQuestionId());
                List<SysFillEntity> fillList=fillService.list(fillwrapper);
                double result=0;
                for (SysFillEntity fill:fillList) {
                    result=fill.getScorevalue();
                }
                if(result>qry.getQuestionScore()){
                    qry.setIsError(1);
                }
            }
            if (qry.getQuestionType()==5){
                QueryWrapper<SysAnswerEntity> answerwrapper = new QueryWrapper<>();
                answerwrapper.eq("id",qry.getQuestionId());
                List<SysAnswerEntity> answerList=answerService.list(answerwrapper);
                double result=0;
                for (SysAnswerEntity answer:answerList) {
                    result=answer.getScorevalue();
                }
                if(result>qry.getQuestionScore()){
                    qry.setIsError(1);
                }
            }
            myExamService.updateMyExam(qry);
            vo.put("code", 200);
            return ResponseEntity.ok(vo);
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    public double format2(double value) {
        DecimalFormat format = new DecimalFormat("#.00");
        String str = format.format(value);
        return Double.parseDouble(str);
    }
    @PostMapping("updateMyExam")
    @Operation(summary = "修改我的考试")
    public ResponseEntity updateMyExam(@RequestBody SysMyExamVO qry){
        Map<String, Object> vo = new HashMap<>();
        try{
            if(qry.getQuestionType()==1){
                double result=0;
                qry.setIsError(0);
                //根据id获取单选题分值
                QueryWrapper<SysRadioEntity> radiowrapper = new QueryWrapper<>();
                radiowrapper.eq("id",qry.getQuestionId());
                List<SysRadioEntity> radioList=radioService.list(radiowrapper);
                for (SysRadioEntity radio:radioList) {
                    result=radio.getScorevalue();
                }
                //根据id获取单选题题目选项以判断答案是否正确
                QueryWrapper<SysRadioDetailEntity> detailwrapper = new QueryWrapper<>();
                detailwrapper.eq("parent_id",qry.getQuestionId());
                detailwrapper.eq("radioresult",1);
                List<SysRadioDetailEntity> detailList=radioDetailService.list(detailwrapper);
                for (SysRadioDetailEntity detail: detailList) {
                    if(detail.getRadioValue().equals(qry.getQuestionResult())){
                    }else {
                        result=0;//答案在错误的情况下
                        qry.setIsError(1);
                    }
                }
                qry.setQuestionScore(result);
            }
            if(qry.getQuestionType()==2){
                //根据id获取多选题分值
                double result=0;
                double score=0;
                double allresult=0;
                qry.setIsError(0);
                QueryWrapper<SysCheckBoxEntity> checkwrapper = new QueryWrapper<>();
                checkwrapper.eq("id",qry.getQuestionId());
                List<SysCheckBoxEntity> checkList=checkBoxService.list(checkwrapper);
                for (SysCheckBoxEntity check:checkList) {
                    result=check.getScorevalue();
                }
                String[] scoreList = qry.getQuestionResult().split(",");
                //根据id获取多选题题目选项以判断答案是否正确
                QueryWrapper<SysCheckBoxDetailEntity> detailwrapper = new QueryWrapper<>();
                detailwrapper.eq("parent_id",qry.getQuestionId());
                detailwrapper.eq("checkboxresult",1);
                List<SysCheckBoxDetailEntity> detailList=checkBoxDetailService.list(detailwrapper);
                score= format2(result/detailList.size());

                for(int value=0;value<scoreList.length;value++){
                    int valcount=0;
                    for (SysCheckBoxDetailEntity detail: detailList) {
                        valcount++;
                        if(detail.getCheckboxValue().equals(scoreList[value])){
                            allresult=allresult+score;
                            valcount++;
                        }else {
                            if(valcount==detailList.size()){
                                allresult=0;
                                qry.setIsError(1);
                            }
                        }
                    }
                }
                if(result!=allresult){
                    qry.setIsError(1);
                }
                qry.setQuestionScore(allresult);
            }
            if(qry.getQuestionType()==3){
                //根据id获取判断题分值
                double result=0;
                qry.setIsError(0);
                QueryWrapper<SysJudgeEntity> checkwrapper = new QueryWrapper<>();
                checkwrapper.eq("id",qry.getQuestionId());
                List<SysJudgeEntity> judgeList=judgeService.list(checkwrapper);
                for (SysJudgeEntity judge:judgeList) {
                    result=judge.getScorevalue();
                }
                //根据id获取判断题目选项以判断答案是否正确
                QueryWrapper<SysJudgeDetailEntity> detailwrapper = new QueryWrapper<>();
                detailwrapper.eq("parent_id",qry.getQuestionId());
                detailwrapper.eq("judgeresult",1);
                List<SysJudgeDetailEntity> detailList=judgeDetailService.list(detailwrapper);
                for (SysJudgeDetailEntity detail: detailList) {
                    if(detail.getJudgeValue().equals(qry.getQuestionResult())){
                    }else {
                        result=0;//答案在错误的情况下
                        qry.setIsError(1);
                    }
                }
                qry.setQuestionScore(result);
            }
            myExamService.updateMyExam(qry);
            vo.put("code", 200);
            return ResponseEntity.ok(vo);
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    @PostMapping("save")
    @Operation(summary = "新增我的考试")
    public ResponseEntity addMyExam(@RequestBody SysMyExamVO qry){
        Map<String, Object> vo = new HashMap<>();
        try{
            if(qry.getQuestionType()==1){
                double result=0;
                qry.setIsError(0);
                //根据id获取单选题分值
                QueryWrapper<SysRadioEntity> radiowrapper = new QueryWrapper<>();
                radiowrapper.eq("id",qry.getQuestionId());
                List<SysRadioEntity> radioList=radioService.list(radiowrapper);
                for (SysRadioEntity radio:radioList) {
                    result=radio.getScorevalue();
                }
                //根据id获取单选题题目选项以判断答案是否正确
                QueryWrapper<SysRadioDetailEntity> detailwrapper = new QueryWrapper<>();
                detailwrapper.eq("parent_id",qry.getQuestionId());
                detailwrapper.eq("radioresult",1);
                List<SysRadioDetailEntity> detailList=radioDetailService.list(detailwrapper);
                for (SysRadioDetailEntity detail: detailList) {
                    if(detail.getRadioValue().equals(qry.getQuestionResult())){
                    }else {
                        result=0;//答案在错误的情况下
                        qry.setIsError(1);
                    }
                }
                qry.setQuestionScore(result);
            }
            if(qry.getQuestionType()==2){
                //根据id获取多选题分值
                double result=0;
                qry.setIsError(0);
                QueryWrapper<SysCheckBoxEntity> checkwrapper = new QueryWrapper<>();
                checkwrapper.eq("id",qry.getQuestionId());
                List<SysCheckBoxEntity> checkList=checkBoxService.list(checkwrapper);
                for (SysCheckBoxEntity check:checkList) {
                    result=check.getScorevalue();
                }
                //根据id获取多选题题目选项以判断答案是否正确
                QueryWrapper<SysCheckBoxDetailEntity> detailwrapper = new QueryWrapper<>();
                detailwrapper.eq("parent_id",qry.getQuestionId());
                detailwrapper.eq("checkboxresult",1);
                List<SysCheckBoxDetailEntity> detailList=checkBoxDetailService.list(detailwrapper);
                boolean isFlag=true;
                for (SysCheckBoxDetailEntity detail: detailList) {
                   if(detail.getCheckboxValue().equals(qry.getQuestionResult())){
                       result= format2(result/detailList.size());
                       isFlag=false;
                   }
                }
                if(detailList.size()>1){
                    qry.setIsError(1);
                    if(isFlag){
                        result=0;
                    }
                }
                qry.setQuestionScore(result);
            }
            if(qry.getQuestionType()==3){
                //根据id获取判断题分值
                double result=0;
                qry.setIsError(0);
                QueryWrapper<SysJudgeEntity> checkwrapper = new QueryWrapper<>();
                checkwrapper.eq("id",qry.getQuestionId());
                List<SysJudgeEntity> judgeList=judgeService.list(checkwrapper);
                for (SysJudgeEntity judge:judgeList) {
                    result=judge.getScorevalue();
                }
                //根据id获取判断题目选项以判断答案是否正确
                QueryWrapper<SysJudgeDetailEntity> detailwrapper = new QueryWrapper<>();
                detailwrapper.eq("parent_id",qry.getQuestionId());
                detailwrapper.eq("judgeresult",1);
                List<SysJudgeDetailEntity> detailList=judgeDetailService.list(detailwrapper);
                for (SysJudgeDetailEntity detail: detailList) {
                    if(detail.getJudgeValue().equals(qry.getQuestionResult())){
                    }else {
                        result=0;//答案在错误的情况下
                        qry.setIsError(1);
                    }
                }
                qry.setQuestionScore(result);
            }
            String id=myExamService.addMyExam(qry);
            vo.put("code", 200);
            vo.put("id", id);
            return ResponseEntity.ok(vo);
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> deleteById(@RequestBody List<Long> idList){
        myExamService.delete(idList);
        return Result.ok();
    }
    @PostMapping("qryMyExamScoreList")
    @Operation(summary = "获取成绩列表")
    public Result<List<SysMyExamVO>> qryMyExamScoreList(@RequestBody SysMyExamQuery query){
        List<SysMyExamVO> list = myExamService.qryMyExamScoreList(query);

        return Result.ok(list);
    }
}

